package com.ruoyi.reservationserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.reservationserver.dao.CourtDao;
import com.ruoyi.reservationserver.entity.Court;
import com.ruoyi.system.domain.Stadium;
import com.ruoyi.reservationserver.service.CourtService;
import com.ruoyi.reservationserver.service.StadiumService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 场地表(Court)表服务实现类
 *
 * @author makejava
 * @since 2023-07-04 16:21:47
 */
@Service("courtService")
public class CourtServiceImpl extends ServiceImpl<CourtDao,Court> implements CourtService {
    @Resource
    private CourtDao courtDao;

    @Resource
    private CourtService courtService;

    @Resource
    private StadiumService stadiumService;



    @Override
    public PageInfo<Court> selectCourtWithPage(Integer page, Integer count) {
        PageHelper.startPage(page, count);

        QueryWrapper<Court> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("court_id");
        List<Court> courtList = courtService.list(queryWrapper);

        for (int i = 0; i < courtList.size(); i++) {
            Court court = courtList.get(i);
            QueryWrapper<Stadium> queryWrapper1=new QueryWrapper<>();

            queryWrapper1.eq("stadium_id",court.getStadiumId());
            Stadium stadium = stadiumService.getOne(queryWrapper1);

            String stadiumName = stadium.getStadiumName();
            court.setStadiumName(stadiumName);

        }
        PageInfo<Court> pageInfo=new PageInfo<>(courtList);
        return pageInfo;
    }


    /**
     * 通过ID查询单条数据
     *
     * @param courtId 主键
     * @return 实例对象
     */
    @Override
    public Court queryById(Integer courtId) {
        return this.courtDao.queryById(courtId);
    }

    @Override
    public Page<Court> queryByPage(Court court, PageRequest pageRequest) {
        return null;
    }


    /**
     * 新增数据
     *
     * @param court 实例对象
     * @return 实例对象
     */
    @Override
    public Court insert(Court court) {
        this.courtDao.insert(court);
        return court;
    }

    /**
     * 修改数据
     *
     * @param court 实例对象
     * @return 实例对象
     */
    @Override
    public Court update(Court court) {
        this.courtDao.update(court);
        return this.queryById(court.getCourtId());
    }



    /**
     * 通过主键删除数据
     *
     * @param courtId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer courtId) {
        return this.courtDao.deleteById(courtId) > 0;
    }
}
